// @ts-nocheck
import { makeObservable, observable, action } from 'mobx';
import Service from './service';
import { IElement, ITask } from './types';
import { message } from 'antd';
import moment from 'moment';
import { getDateTime } from '@/utils';

export default class Store {

  readonly service: Service;

  @observable
  room_id: string | undefined = undefined;

  @observable
  elements: IElement[] = [];

  @observable
  data: ITask[] = [];

  @observable
  loading: boolean = false;

  @observable
  dialogLoading: boolean = false;

  @observable
  visible: boolean = false;

  constructor() {
    makeObservable(this);
    this.service = new Service();
  }

  @action.bound
  search() {
    this.service.search({ room_id: this.room_id }).then(data => {
      this.setData(data);
    })
  }

  @action.bound
  setData(response: any) {
    const { elements, tasks } = response ?? {};
    let beginDate: Date = null;
    let endDate: Date = null;
    const data: ITask[] = tasks.filter(item => !!item.group_id).map(item => {
      if (!beginDate || moment(item.work_start).isBefore(moment(beginDate))) {
        beginDate = new Date(item.work_start);
      }
      if (!endDate || moment(item.work_end).isAfter(moment(endDate))) {
        endDate = new Date(item.work_end);
      }
      return {
        ...item,
        color: item.device_color || item.order_color,
        start_date: item.work_start,
        end_date: item.work_end,
        section_id: item.work_device_id,
        text: item.mbom_num,
      }
    });
    // this.beginDate = beginDate;
    // this.endDate = endDate;

    const groupList: IElement[] = [];
    elements?.forEach(ele => {
      if (groupList.findIndex(item => item.key === ele.room_id) === -1) {
        // 车间
        const room: IElement = {
          id: ele.roo_id,
          key: ele.room_id,
          label: ele.room_name,
          text: ele.room_name,
          open: true,
          children: elements.filter(e => e.room_id === ele.room_id).map(item => {
            // 作业区
            const { id, label } = item;
            const resourceGroup: IElement = {
              key: id,
              id,
              label: label,
              text: label,
              open: true,
              children: (item.children || []).map(device => ({
                id: device.id,
                key: device.id,
                label: device.device_num + `(` + device.device_name + `)`,
              }))
            };

            return resourceGroup;
          }).filter(item => item.children?.length)
        }

        groupList.push(room);
      }
    })
    const result = [];
    groupList.filter(item => item.children?.length).forEach(group => {
      const { children: resourceGroupList } = group;
      resourceGroupList?.forEach(child => {
        const { children, ...others } = child;
        const resourceIds = children?.map(item => item.key);
        const groupTasks = tasks.filter(item => resourceIds?.includes(item.work_device_id));
        result.push({
          ...others,
          start_date: groupTasks[0]?.work_start || getDateTime(),
          end_date: groupTasks[0]?.work_start || getDateTime(),
          hide_bar: true,
          hide: true,
          hidden: true,
          class: 'hide-task',
          // duration: 0
        });
        children?.forEach(node => {
          const children = groupTasks.filter(item => item.work_device_id === node.key).map(task => ({
            id: task.id,
            text: task.mbom_num,
            start_date: task.work_start,
            end_date: task.work_end,
            ...task,
          }));
          result.push({
            id: node.id, text: node.label,
            parent: child.id,
            start_date: children[0]?.work_start || getDateTime(),
            end_date: children[children.length - 1]?.work_end || getDateTime(),
            hide_bar: true,
            hide: true,
            hidden: true,
            class: 'hide-task',
            type: 'project',
            render: 'split',
            open: false,
          });
          children.forEach((task, index) => {
            result.push({
              ...task,
              id: task.id,
              parent: node.id,
              text: `${task.batch_order_num} #${task.mbom_num}`,
              start_date: task.work_start,
              end_date: task.work_end,
              color: task.order_color,
              open: true,
            });
          })
        })
      })
    });
    this.elements = result;
  }

  @action.bound
  onChange(room_id: string) {
    this.room_id = room_id;
    this.search();
  }

  @action.bound
  show() {
    this.visible = true;
  }

  @action.bound
  close() {
    this.visible = false;
  }

  @action.bound
  submit(payload: any) {
    this.dialogLoading = true;
    this.service.assignmentTask(payload).then(() => {
      message.success('提交成功');
      this.close();
      this.search();
    }).finally(() => {
      this.dialogLoading = false;
    })
  }
}
